/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanViews;

import entity.Category;
import entity.Complaint;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import processor.CategoryFacade;
import processor.ComplaintFacade;
import processor.MembersFacade;

/**
 *
 * @author maynoigi
 */
@ManagedBean
@SessionScoped
public class Profile implements Serializable {

    @EJB
    private CategoryFacade categoryFacade;
    @EJB
    private MembersFacade membersFacade;
    @EJB
    private ComplaintFacade complaintFacade;
    private String username;
    List<Complaint> complaints;
    String oldPassword;
    String newPassword;
    String confirmPassword;
    String alert = "a";
    String alertEdit = "a";

    public String getAlertEdit() {
        return alertEdit;
    }

    public void setAlertEdit(String alertEdit) {
        this.alertEdit = alertEdit;
    }
    Complaint complaintSelected = new Complaint();
    String title;
    String content;
    String priority;

    public LinkedHashMap<String, String> getCategorys() {
        List<Category> temp = categoryFacade.getCategoryAll();
        for (Category category : temp) {

            categorys.put(category.getCategoryName(), category.getCategoryPriority());
        }
      return categorys;
    }

    public void setCategorys(LinkedHashMap<String, String> categorys) {
        this.categorys = categorys;
    }

    public String getCategorySelected() {
        categorySelected = complaintSelected.getComplaintPriority();
        return categorySelected;
    }

    public void setCategorySelected(String categorySelected) {
        this.categorySelected = categorySelected;
    }
    private LinkedHashMap<String, String> categorys = new LinkedHashMap<String, String>();
    private String categorySelected;

    public String getTitle() {
        title = complaintSelected.getComplaintTitle();
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        content = complaintSelected.getComplaintContent();
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public Complaint getComplaintSelected() {

        return complaintSelected;
    }

    public void setComplaintSelected(Complaint complaintSelected) {
        this.complaintSelected = complaintSelected;

    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getAlert() {
        return alert;
    }

    public void setAlert(String alert) {
        this.alert = alert;
    }
    @ManagedProperty(value = "#{login}")//this is EL name of your bean 
    private Login injectedBean;

    public Profile() {
    }

    public String getUsername() {

        username = injectedBean.getCustomer().getMembersName();
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<Complaint> getComplaints() {

        complaints = complaintFacade.getComplaintByMembers(injectedBean.getCustomer());
        return complaints;
    }

    public void setComplaints(List<Complaint> complaints) {
        this.complaints = complaints;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public Login getInjectedBean() {
        return injectedBean;
    }

    public void setInjectedBean(Login injectedBean) {
        this.injectedBean = injectedBean;
    }

    public void editComplaint() {

        complaintSelected.setComplaintContent(content);
        complaintSelected.setComplaintTitle(title);
        complaintSelected.setComplaintPriority(categorySelected);
        complaintFacade.edit(complaintSelected);
        alertEdit = "Edit Complaint \"" + complaintSelected.getComplaintTitle() + "\" Succesfull";
    }

    public void deleteComplaint(Complaint temp) {
        complaintFacade.deleteComplaints(temp);
    }

    public String directEdit(Complaint temp) {
        complaintSelected = temp;
        alertEdit = "a";
        return "EditComplaints.xhtml";
    }

    public String changePassword() {

        if (!confirmPassword.equals(newPassword)) {
            alert = "Confirm password is invalid";
            return "Profile.xhtml";
        } else {
            boolean temp = membersFacade.changePassword(oldPassword, newPassword, injectedBean.getCustomer().getMembersID());
            if (temp) {
                alert = "Change Password Successfull";
                return "Profile.xhtml";
            } else {
                alert = "Old Password Invalid";
                return "Profile.xhtml";
            }
        }
    }
}
